package com.yazi.mxz.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.hibernate.dao.BasicService;
import com.myframework.smhj.hibernate.dao.Dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yazi.mxz.common.db.dao.SpecTypeDao;
import com.yazi.mxz.common.db.dao.StoreGoodsDao;
import com.yazi.mxz.common.entity.ValueFactory;
import com.yazi.mxz.common.entity.beans.Category;
import com.yazi.mxz.common.entity.beans.Company;
import com.yazi.mxz.common.entity.beans.Goods;
import com.yazi.mxz.common.entity.beans.SpecType;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.yazi.mxz.common.service.SpecTypeService;

/**
 * Service实现：商品规格类型
 *
 * @author mo_yq5
 * @since 2016年6月1日
 */
@Service("specTypeService")
public class SpecTypeServiceImpl extends BasicService<SpecType> implements SpecTypeService {
	private static Logger log = LoggerFactory.getLogger(SpecTypeServiceImpl.class);
	
	@Resource
	SpecTypeDao specTypeDao;
	@Resource
	StoreGoodsDao storeGoodsDao;

	@Override
	protected Dao<SpecType> getDao() {
		return specTypeDao;
	}
	
	@Override
	public List<SpecType> list(Long catId, Long storeGoodsId) {
		log.debug("list: catId={},storeGoodsId={}", catId, storeGoodsId);
		List<SpecType> list = new ArrayList<SpecType>();
		SpecType specType = new SpecType();
		
		// 商品类目下的
		if (null != catId) {
			specType = new SpecType();
			Category cat = new Category();
			cat.setCatId(catId);
			specType.setCategory(cat);
			List<SpecType> list2 = specTypeDao.listByExample(specType);
			if (list2.size() > 0) {
				list.addAll(list2);
			}
		}
		
		// 店铺商品、企业商品下的
		if (null != storeGoodsId) {
			
			// StoreGoods
			specType = new SpecType();
			StoreGoods sg = new StoreGoods();
			sg.setStoreGoodsId(storeGoodsId);
			specType.setStoreGoods(sg);
			specTypeDao.listByExample(specType);
			List<SpecType> list2 = specTypeDao.listByExample(specType);
			if (list2.size() > 0) {
				list.addAll(list2);
			}
			
			// Goods
			specType = new SpecType();
			Goods goods = storeGoodsDao.get(storeGoodsId).getGoods();
			specType.setGoods(goods);
			specTypeDao.listByExample(specType);
			List<SpecType> list3 = specTypeDao.listByExample(specType);
			if (list3.size() > 0) {
				list.addAll(list3);
			}
		}
		
		return list;
	}

	/**
	 * 本不方法未检查参数对象的合法性，因此在调用此方法前请自行做好合法性检查
	 */
	@Override
	public SpecType addOrEdit(SpecType specType) throws MyException {
		SpecType dest;
		final Long specTypeId = specType.getSpecTypeId();
		if (null == specTypeId) {// 添加
			log.debug("addOrEdit->add：name={}", specType.getName());
			ValueFactory.validate(specType);
			specType.setSpecTypeId(null);
			specType.setUpdateTime(DateUtil.getTimeStamp());
			dest = specTypeDao.persist(specType);
		} else {// 编辑
			log.debug("addOrEdit->edit：specTypeId={}, name={}", specTypeId, specType.getName());
			dest = specTypeDao.get(specTypeId);
			ValueFactory.combine(specType, dest);
			dest.setUpdateTime(DateUtil.getTimeStamp());
			specTypeDao.merge(dest);
		}
		return dest;
	}
	
	@Override
	public SpecType addOrEditByCompany(Company company, SpecType specType) throws MyException {
		if (null == specType.getSpecTypeId()) {// 添加
			// 商品必须是指定企业的
			if (!specType.getGoods().getCompany().equals(company)) {
				throw new MyException();
			}
		} else {// 编辑
			// 如果重新指定归属商品，商品必须是指定企业的
			if (null != specType.getGoods() && !specType.getGoods().getCompany().equals(company)) {
				throw new MyException();
			}
		}
		return addOrEdit(specType);
	}
	
	@Override
	public SpecType addOrEditByStore(Store store, SpecType specType) throws MyException {
		if (null == specType.getSpecTypeId()) {// 添加
			// 店铺商品必须是指定店铺的
			if (!specType.getStoreGoods().getStore().equals(store)) {
				throw new MyException();
			}
		} else {// 编辑
			// 如果重新指定归属店铺商品，店铺商品必须是指定店铺的
			if (null != specType.getStoreGoods() && !specType.getStoreGoods().getStore().equals(store)) {
				throw new MyException();
			}
		}
		return addOrEdit(specType);
	}
	
}
